/*
 * Copyright (C) 2020 The LINN Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.juqimiao.linn.domain;

import com.google.common.hash.HashCode;
import com.google.j2objc.annotations.Weak;
import com.juqimiao.linn.context.BusinessPoint;

import lombok.Data;
import lombok.Getter;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;

import java.io.Serializable;

/**
 * 领域实体基类。
 *
 * @author Collin Cai
 *
 * Todo: 实现值对比功能的基本算法。
 * Todo: 实现领域实体类的周期状态高效维持机制。 Cachable?
 */
@Data
@DomainObject
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public abstract class Entity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Weak
    private BusinessPoint businessPoint;
    /**
     * 业务ID
     */
    @Getter
    private String id;
    /**
     * 领域全局系统中唯一标识。
     * 比如：全球唯一标识
     */
    @Getter
    private String uid;

    public void setId(String id){
        this.id = id;
        if(idConsistent) {
            this.uid = id;
        }
    }
    public void setUid(String uid){
        this.uid = uid;
        if(this.idConsistent){
            this.id = uid;
        }
    }

    private boolean idConsistent = true;

    /** Todo: 通过被打了特定注解的Field进行重写 hashCode和equals. 只支持 primitive type。  */
    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }
}

